list box: Update accessible implementation
authorMatthias Clasen <mclasen@redhat.com>
Sat, 29 Mar 2014 02:10:27 +0000 (22:10 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 6 Apr 2014 05:44:36 +0000 (01:44 -0400)
Now that multi selection is supported, we can provide a more
complete AtkSelection implementation.

gtk/a11y/gtklistboxaccessible.c
gtk/a11y/gtklistboxaccessibleprivate.h
gtk/a11y/gtklistboxrowaccessible.c

index f4f269048919fd528991f345e51a4ce710e49486..3343dc925c189545d26acdecad6a3b9e8a01cd34 100644 (file)
@@ -84,6 +84,26 @@ gtk_list_box_accessible_add_selection (AtkSelection *selection,
   return FALSE;
 }
 
+static gboolean
+gtk_list_box_accessible_remove_selection (AtkSelection *selection,
+                                          gint          idx)
+{
+  GtkWidget *box;
+  GtkListBoxRow *row;
+
+  box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
+  if (box == NULL)
+    return FALSE;
+
+  row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (box), idx);
+  if (row)
+    {
+      gtk_list_box_unselect_row (GTK_LIST_BOX (box), row);
+      return TRUE;
+    }
+  return FALSE;
+}
+
 static gboolean
 gtk_list_box_accessible_clear_selection (AtkSelection *selection)
 {
@@ -93,49 +113,92 @@ gtk_list_box_accessible_clear_selection (AtkSelection *selection)
   if (box == NULL)
     return FALSE;
 
-  gtk_list_box_select_row (GTK_LIST_BOX (box), NULL);
+  gtk_list_box_unselect_all (GTK_LIST_BOX (box));
   return TRUE;
 }
 
+static gboolean
+gtk_list_box_accessible_select_all (AtkSelection *selection)
+{
+  GtkWidget *box;
+
+  box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
+  if (box == NULL)
+    return FALSE;
+
+  gtk_list_box_select_all (GTK_LIST_BOX (box));
+  return TRUE;
+}
+
+typedef struct
+{
+  gint idx;
+  GtkWidget *row;
+} FindSelectedData;
+
+static void
+find_selected_row (GtkListBox    *box,
+                   GtkListBoxRow *row,
+                   gpointer       data)
+{
+  FindSelectedData *d = data;
+
+  if (d->idx == 0)
+    {
+      if (d->row == NULL)
+        d->row = GTK_WIDGET (row);
+    }
+  else
+    d->idx -= 1;
+}
+
 static AtkObject *
 gtk_list_box_accessible_ref_selection (AtkSelection *selection,
                                        gint          idx)
 {
   GtkWidget *box;
-  GtkListBoxRow *row;
   AtkObject *accessible;
-
-  if (idx != 0)
-    return NULL;
+  FindSelectedData data;
 
   box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
   if (box == NULL)
     return NULL;
 
-  row = gtk_list_box_get_selected_row (GTK_LIST_BOX (box));
-  if (row == NULL)
+  data.idx = idx;
+  data.row = NULL;
+  gtk_list_box_selected_foreach (GTK_LIST_BOX (box), find_selected_row, &data);
+
+  if (data.row == NULL)
     return NULL;
 
-  accessible = gtk_widget_get_accessible (GTK_WIDGET (row));
+  accessible = gtk_widget_get_accessible (data.row);
   g_object_ref (accessible);
   return accessible;
 }
 
+static void
+count_selected (GtkListBox    *box,
+                GtkListBoxRow *row,
+                gpointer       data)
+{
+  gint *count = data;
+  *count += 1;
+}
+
 static gint
 gtk_list_box_accessible_get_selection_count (AtkSelection *selection)
 {
   GtkWidget *box;
-  GtkListBoxRow *row;
+  gint count;
 
   box = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
   if (box == NULL)
     return 0;
 
-  row = gtk_list_box_get_selected_row (GTK_LIST_BOX (box));
-  if (row == NULL)
-    return 0;
+  count = 0;
+  gtk_list_box_selected_foreach (GTK_LIST_BOX (box), count_selected, &count);
 
-  return 1;
+  return count;
 }
 
 static gboolean
@@ -149,20 +212,20 @@ gtk_list_box_accessible_is_child_selected (AtkSelection *selection,
   if (box == NULL)
     return FALSE;
 
-  row = gtk_list_box_get_selected_row (GTK_LIST_BOX (box));
-  if (row == NULL)
-    return FALSE;
+  row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (box), idx);
 
-  return row == gtk_list_box_get_row_at_index (GTK_LIST_BOX (box), idx);
+  return gtk_list_box_row_is_selected (row);
 }
 
 static void atk_selection_interface_init (AtkSelectionIface *iface)
 {
   iface->add_selection = gtk_list_box_accessible_add_selection;
+  iface->remove_selection = gtk_list_box_accessible_remove_selection;
   iface->clear_selection = gtk_list_box_accessible_clear_selection;
   iface->ref_selection = gtk_list_box_accessible_ref_selection;
   iface->get_selection_count = gtk_list_box_accessible_get_selection_count;
   iface->is_child_selected = gtk_list_box_accessible_is_child_selected;
+  iface->select_all_selection = gtk_list_box_accessible_select_all;
 }
 
 void
index da3563b0956c119eff9b624a5851586acfa164b8..61ecea339ff0a6488c4b0944c80f234b5a482d00 100644 (file)
@@ -22,8 +22,9 @@
 
 G_BEGIN_DECLS
 
-void _gtk_list_box_accessible_update_cursor   (GtkListBox *box, GtkListBoxRow *child);
-void _gtk_list_box_accessible_selection_changed (GtkListBox *box);
+void _gtk_list_box_accessible_update_cursor     (GtkListBox    *box,
+                                                 GtkListBoxRow *row);
+void _gtk_list_box_accessible_selection_changed (GtkListBox    *box);
 
 G_END_DECLS
 
index 36e84a728d8aa966740fdc1cba8376705a2b5724..d6812dfaaa7a8f0e0091960fae49d367e37cff9f 100644 (file)
@@ -53,7 +53,7 @@ gtk_list_box_row_accessible_ref_state_set (AtkObject *obj)
       if (gtk_list_box_get_selection_mode (GTK_LIST_BOX (parent)) != GTK_SELECTION_NONE)
         atk_state_set_add_state (state_set, ATK_STATE_SELECTABLE);
 
-      if (widget == (GtkWidget*)gtk_list_box_get_selected_row (GTK_LIST_BOX (parent)))
+      if (gtk_list_box_row_is_selected (GTK_LIST_BOX_ROW (widget)))
         atk_state_set_add_state (state_set, ATK_STATE_SELECTED);
     }